package com.microcdyUser.service.impl;
import com.microcdyUser.dao.SysUserPostMapper;
import com.microcdy.sys.pojo.SysUserPost;
import com.microcdyUser.service.SysUserPostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:cdy
 * @Description:SysUserPost业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SysUserPostServiceImpl implements SysUserPostService {

    @Autowired
    private SysUserPostMapper sysUserPostMapper;


    /**
     * SysUserPost条件+分页查询
     * @param sysUserPost 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SysUserPost> findPage(SysUserPost sysUserPost, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sysUserPost);
        //执行搜索
        return new PageInfo<SysUserPost>(sysUserPostMapper.selectByExample(example));
    }

    /**
     * SysUserPost分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SysUserPost> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SysUserPost>(sysUserPostMapper.selectAll());
    }

    /**
     * SysUserPost条件查询
     * @param sysUserPost
     * @return
     */
    @Override
    public List<SysUserPost> findList(SysUserPost sysUserPost){
        //构建查询条件
        Example example = createExample(sysUserPost);
        //根据构建的条件查询数据
        return sysUserPostMapper.selectByExample(example);
    }


    /**
     * SysUserPost构建查询对象
     * @param sysUserPost
     * @return
     */
    public Example createExample(SysUserPost sysUserPost){
        Example example=new Example(SysUserPost.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysUserPost!=null){
            // 用户ID
            if(!StringUtils.isEmpty(sysUserPost.getUserId())){
                    criteria.andEqualTo("userId",sysUserPost.getUserId());
            }
            // 岗位ID
            if(!StringUtils.isEmpty(sysUserPost.getPostId())){
                    criteria.andEqualTo("postId",sysUserPost.getPostId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        sysUserPostMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SysUserPost
     * @param sysUserPost
     */
    @Override
    public void update(SysUserPost sysUserPost){
        sysUserPostMapper.updateByPrimaryKey(sysUserPost);
    }

    /**
     * 增加SysUserPost
     * @param sysUserPost
     */
    @Override
    public void add(SysUserPost sysUserPost){
        sysUserPostMapper.insert(sysUserPost);
    }

    /**
     * 根据ID查询SysUserPost
     * @param id
     * @return
     */
    @Override
    public SysUserPost findById(Long id){
        return  sysUserPostMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SysUserPost全部数据
     * @return
     */
    @Override
    public List<SysUserPost> findAll() {
        return sysUserPostMapper.selectAll();
    }
}
